const Blocks = require('../model/blocks.model');

// 使用模糊查询需要先引入Op
const seq = require('sequelize');
const Op = seq.Op;

// 服务层 读写数据库
class BlocksService {
    // 添加物品
    async createBlocks(blockId, blockName, blockDesc, price, blockType, pileNumber, blockPath, relationItemId, relationItemType, totalGrowDay, repeatPickDay, statusUI, composition) {
        // Blocks.create({
        //     blockName: blockName,
        //     blockDesc: blockDesc
        // });
        // 属性和值一样可以简写
        let res = await Blocks.create({
            blockId,
            blockName,
            blockDesc,
            price,
            blockType,
            pileNumber,
            blockPath,
            relationItemId,
            relationItemType,
            composition, totalGrowDay, repeatPickDay, statusUI,
        });
        return res;
    }

    async getBlocksInfo({id, blockName, blockType, blockDesc, flag, pageInfo}) {
        let whereObj = {};
        id && Object.assign(whereObj, {id});
        blockDesc && Object.assign(whereObj, {blockDesc});
        blockType && Object.assign(whereObj, {blockType});
        flag && Object.assign(whereObj, {flag});

        blockName && Object.assign(whereObj, {
            blockName: {
                [Op.like]: `%${blockName}%`
            },
        });
        // 查数量
        let count = await Blocks.count({where: whereObj});
        let res;
        if (count !== 0) {
            let _pageInfo = {};
            if (pageInfo) {
                let {pageIndex, pageSize} = JSON.parse(pageInfo);
                _pageInfo = {
                    limit: pageSize,
                    offset: (pageIndex - 1) * pageSize
                }
            }
            res = await Blocks.findAll({
                where: whereObj,
                ..._pageInfo
            });
        }
        return {list: res, count: count} || null;
    }

    async updateBlocksInfo({id, blockId, blockName, blockDesc, price, blockType, pileNumber, blockPath, relationItemId, relationItemType, totalGrowDay, repeatPickDay, statusUI, composition, flag}) {

        let whereObj = {id};
        let newBlocks = {};
        blockId && Object.assign(newBlocks, {blockId});
        blockName && Object.assign(newBlocks, {blockName});
        blockDesc && Object.assign(newBlocks, {blockDesc});
        price && Object.assign(newBlocks, {price});
        blockType && Object.assign(newBlocks, {blockType});
        pileNumber && Object.assign(newBlocks, {pileNumber});
        blockPath && Object.assign(newBlocks, {blockPath});
        relationItemId && Object.assign(newBlocks, {relationItemId});
        relationItemType && Object.assign(newBlocks, {relationItemType});
        totalGrowDay && Object.assign(newBlocks, {totalGrowDay});
        repeatPickDay && Object.assign(newBlocks, {repeatPickDay});
        statusUI && Object.assign(newBlocks, {statusUI});
        composition && Object.assign(newBlocks, {composition});
        flag && Object.assign(newBlocks, {flag});
        let res = await Blocks.update(newBlocks, {
            where: whereObj
        });
        return res[0] > 0;
    }
}

module.exports = new BlocksService();